package com.dqhl.community.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.CommunityNotice;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.util.TimeUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by LIENLIN on 2016/8/31.
 * 社区公告详情页
 */
public class CommunityNoticeDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mIvback;
    private TextView mTvTitle;
    private LinearLayout mLinearFragment;

    private TextView tv_title;
    private TextView tv_time;
    private ImageView iv_picture;
    private TextView tv_content;

    private ScrollView sv_content;
    private TextView tv_no_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community_notice_detail);

        initView();

        getData();
    }

    private void getData() {
        showCustomLoadBar("加载中……");
        String noticeId = getIntent().getStringExtra("notice_id");

        RequestParams params = new RequestParams(Config.Api.noticeContent);
        params.addBodyParameter("id", noticeId);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    CommunityNotice communityNotice = JSON.parseObject(data, CommunityNotice.class);
                    Dlog.e(communityNotice.toString());

                    tv_title.setText(communityNotice.getTitle());
                    String time = TimeUtils.timeStamp2Date(communityNotice.getAdd_time(), "MM月dd日 HH:mm");
                    tv_time.setText(time);
                    String picPath = communityNotice.getPicture();
                    if (!"".equals(picPath) && picPath.length() >= 0) {
                        x.image().bind(iv_picture, Config.Api.base_img_url + picPath);
                    } else {
                        iv_picture.setVisibility(View.GONE);
                    }
                    tv_content.setText(Html.fromHtml(communityNotice.getContent()));
//                    tv_content.setText(communityNotice.getContent());
                } else {
                    sv_content.setVisibility(View.GONE);
                    tv_no_info.setVisibility(View.VISIBLE);
                    tv_no_info.setText(R.string.error_server_connection);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                sv_content.setVisibility(View.GONE);
                tv_no_info.setVisibility(View.VISIBLE);
                tv_no_info.setText(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_top_back);
        mTvTitle = (TextView) findViewById(R.id.tv_top_title);
        mLinearFragment = (LinearLayout) findViewById(R.id.linear_community_content);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_time = (TextView) findViewById(R.id.tv_time);
        iv_picture = (ImageView) findViewById(R.id.iv_picture);
        tv_content = (TextView) findViewById(R.id.tv_content);
        sv_content = (ScrollView) findViewById(R.id.sv_content);
        tv_no_info = (TextView) findViewById(R.id.tv_no_info);

        mIvback.setVisibility(View.VISIBLE);
        mIvback.setOnClickListener(this);
        mTvTitle.setText(R.string.tv_notice_top_title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }
}
